<?php
if(isset($_POST)){
    $to = $_POST['pUrl'];
    $courtDisplayName = $_POST['courtDisplayName'];
    $date = $_POST['date'];
    $startTime = $_POST['startTime'];
    $endTime = $_POST['endTime'];
    $courtAddress = $_POST['courtAddress'];
    $Money = $_POST['pM'];
    $cartId = $_POST['cI'];
    $jq_openid = $_POST['jq_openid'];//用户相对应环奥体育的openid
    $TotalMoney = $Money * 100;//单位换算为分   
    $attach = $cartId."#".$jq_openid."#".$courtDisplayName."#".$date."#".$startTime."#".$endTime."#".$courtAddress."#".$Money;
}
//echo $wxPayHelper->create_biz_package();

?> 
<!DOCTYPE html>
<html>
    <head>
         <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
          <TITLE>微信安全支付</TITLE>
		 <LINK rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
		<SCRIPT src="http://code.jquery.com/jquery-1.10.2.min.js"></SCRIPT>
		<SCRIPT src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></SCRIPT>
        <script language="javascript" src="js/jquery.js"></script>
        <script language="javascript" src="js/lazyloadv3.js"></script>
        <script src="js/md5.js"></script>
        <script src="js/sha1.js"></script>
        <script Language="javascript">
            function getPartnerId()
            {
				return "1220336601";
            }
            
            function getPartnerKey()
            {
				return "89003dcbc62cb9cbdd6a2eb5aded04de";
            }
			
            function getAppId()
            {
				return "wx5f071f58065f81ce";
            }
            
            function getAppKey()
            {
				return "LDs5XBBXrDOI5U8KcGkH6AAE26lTO5ke1QNiB6hoYaMSBaKBlgnIn3So6z0C9vRO5l2Sm00lMTOVbl9RLI5bJ6LvEQ5mczRH6iK4cycBxwBiiPiVQQI6KapwLh6gdc48";
            }		
		
            //辅助函数
            function Trim(str,is_global)
            {
                var result;
                result = str.replace(/(^\s+)|(\s+$)/g,"");
                if(is_global.toLowerCase()=="g") result = result.replace(/\s/g,"");
                return result;
            }
            function clearBr(key)
            {
                key = Trim(key,"g");
                key = key.replace(/<\/?.+?>/g,"");
                key = key.replace(/[\r\n]/g, "");
                return key;
            }
            
            //获取随机数
            function getANumber()
            {
                var date = new Date();
                var times1970 = date.getTime();
                var times = date.getDate() + "" + date.getHours() + "" + date.getMinutes() + "" + date.getSeconds();
                var encrypt = times * times1970;
                if(arguments.length == 1){
                    return arguments[0] + encrypt;
                }else{
                    return encrypt;
                }
                
            }

            //以下是package组包过程：
            
            var oldPackageString;//记住package，方便最后进行整体签名时取用
           
            function getPackage()
            {
                var attach = "<?php echo $attach; ?>";
                var banktype = "WX";
                var body = "<?php echo $courtDisplayName;?>";//商品名称信息，这里由测试网页填入。
                var fee_type = "1";//费用类型，这里1为默认的人民币
                var input_charset = "UTF-8";//字符集，可以使用GBK或者UTF-8
                var notify_url = "http://www.hualvhb.cn/pay/weixin/notice.php";//支付成功后将通知该地址
                var out_trade_no = ""+getANumber();//订单号，商户需要保证该字段对于本商户的唯一性
                var partner = getPartnerId();//测试商户号
                var spbill_create_ip = "127.0.0.1";//用户浏览器的ip，这个需要在前端获取。这里使用127.0.0.1测试值
                var total_fee = <?php echo $TotalMoney;?>;//总金额。
               
                
                var partnerKey = getPartnerKey();//这个值和以上其他值不一样是：签名需要它，而最后组成的传输字符串不能含有它。这个key是需要商户好好保存的。
                
                //首先第一步：对原串进行签名，注意这里不要对任何字段进行编码。这里是将参数按照key=value进行字典排序后组成下面的字符串,在这个字符串最后拼接上key=XXXX。由于这里的字段固定，因此只需要按照这个顺序进行排序即可。
                var signString = "attach="+attach+"&bank_type="+banktype+"&body="+body+"&fee_type="+fee_type+"&input_charset="+input_charset+"&notify_url="+notify_url+"&out_trade_no="+out_trade_no+"&partner="+partner+"&spbill_create_ip="+spbill_create_ip+"&total_fee="+total_fee+"&key="+partnerKey;
                
                var md5SignValue =  ("" + CryptoJS.MD5(signString)).toUpperCase();
                //然后第二步，对每个参数进行url转码，如果您的程序是用js，那么需要使用encodeURIComponent函数进行编码。
                
                attach = encodeURIComponent(attach);
                banktype = encodeURIComponent(banktype);
                body=encodeURIComponent(body);
                fee_type=encodeURIComponent(fee_type);
                input_charset = encodeURIComponent(input_charset);
                notify_url = encodeURIComponent(notify_url);
                out_trade_no = encodeURIComponent(out_trade_no);
                partner = encodeURIComponent(partner);
                spbill_create_ip = encodeURIComponent(spbill_create_ip);
                total_fee = encodeURIComponent(total_fee);
                
                
                //然后进行最后一步，这里按照key＝value除了sign外进行字典序排序后组成下列的字符串,最后再串接sign=value
                var completeString = "attach="+attach+"&bank_type="+banktype+"&body="+body+"&fee_type="+fee_type+"&input_charset="+input_charset+"&notify_url="+notify_url+"&out_trade_no="+out_trade_no+"&partner="+partner+"&spbill_create_ip="+spbill_create_ip+"&total_fee="+total_fee;
                completeString = completeString + "&sign="+md5SignValue;
                
                
                oldPackageString = completeString;//记住package，方便最后进行整体签名时取用
                
                return completeString;
            }
            
            
            
            //下面是app进行签名的操作：
            
            var oldTimeStamp ;//记住timestamp，避免签名时的timestamp与传入的timestamp时不一致
            var oldNonceStr ; //记住nonceStr,避免签名时的nonceStr与传入的nonceStr不一致
                     
            function getTimeStamp()
            {
                var timestamp=new Date().getTime();
                var timestampstring = timestamp.toString();//一定要转换字符串
                oldTimeStamp = timestampstring;
                return timestampstring;
            }
            
            function getNonceStr()
            {
                var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                var maxPos = $chars.length;
                var noceStr = "";
                for (i = 0; i < 32; i++) {
                    noceStr += $chars.charAt(Math.floor(Math.random() * maxPos));
                }
                oldNonceStr = noceStr;
                return noceStr;
            }
            
            function getSignType()
            {
                return "SHA1";
            }
            
            function getSign()
            {
                var app_id = getAppId().toString();
                var app_key = getAppKey().toString();
                var nonce_str = oldNonceStr;
                var package_string = oldPackageString;
                var time_stamp = oldTimeStamp;
                //第一步，对所有需要传入的参数加上appkey作一次key＝value字典序的排序
                var keyvaluestring = "appid="+app_id+"&appkey="+app_key+"&noncestr="+nonce_str+"&package="+package_string+"&timestamp="+time_stamp;
                sign = CryptoJS.SHA1(keyvaluestring).toString();
                return sign;
            }
            
            </script>
        <meta id="viewport" name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1; user-scalable=no;" />
    </head>
<body>
    <div data-role="page" id="page1">
	  <div data-role="header">
	   <h1></h1>
	  </div>
      <div data-role="content">
        <UL data-role="listview" data-inset="true">
          <LI>
            <P>
              <div class="fieldcontain">
                <label>场地名称：</label>
                <input value="<?php echo $courtDisplayName;?>" type="text" readonly="readonly" >
              </div>
              <div class="fieldcontain">
                <label>预定时间：</label>
                <input  value="<?php echo $date; ?>&nbsp;&nbsp;<?php echo $startTime; ?>-<?php echo $endTime; ?>" type="text" readonly="readonly" >
              </div>
                <div class="fieldcontain">
                <label>详细地址：</label>
                <input value="<?php echo $courtAddress;?>" type="text" readonly="readonly" >
              </div>
              <div class="fieldcontain">
                <label>支付总金额：</label>
                <input name="paid_money" id="paid_money" value="<?php echo $Money ?>元" type="text" readonly="readonly">
                <input type="hidden" id="url" value="<?php echo $to;?>">
                <input type="hidden" id="cartId" value="<?php echo $cartId;?>">
              </div>
                <div class="fieldcontain">
                <a  data-role="button" id="getBrandWCPayRequest" href="javascript:void(0);">确认支付</a>
              </div>
            </P>
          </LI>
        </UL>			  
      </div>
	<div data-theme="a" data-role="footer" data-position="fixed">
	<h3>比邻平台</h3>
	</div>
    </div>
	<script language="javascript">            
	
	// 当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
		   WeixinJSBridge.call('hideOptionMenu');
		  //公众号支付
		  jQuery('a#getBrandWCPayRequest').click(function(e){
								 WeixinJSBridge.invoke('getBrandWCPayRequest',{
													   "appId" : getAppId(), //公众号名称，由商户传入
													   "timeStamp" : getTimeStamp(), //时间戳
													   "nonceStr" : getNonceStr(), //随机串
													   "package" : getPackage(),//扩展包
													   "signType" : getSignType(), //微信签名方式:1.sha1
													   "paySign" : getSign() //微信签名
													   },function(res){
													   if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                                                                                                                        var x= document.getElementById("url").value;
															location.href = x; 
                                                                                                           }
													   }); 
												 
												 });
		  WeixinJSBridge.log('pay ready.');
		  
		  }, false)
	</script>
</body>
</html>